﻿using B.S.Data.MES.API.Applications.WorkOrderManagement.Commands;
using B.S.Domain.Ticket;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using System.Transactions;

namespace B.S.Data.MES.API.Applications.WorkOrderManagement.CommandHandlers
{
    public class AllDeleteWorkOrderCommandHandler : IRequestHandler<AllDeleteWorkOrderCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<WorkOrder> _workorderRepository;
        public AllDeleteWorkOrderCommandHandler(IBaseRepository<WorkOrder> workorderRepository)
        {
            _workorderRepository = workorderRepository;
        }
        public async Task<APIRequst<int>> Handle(AllDeleteWorkOrderCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> result = new APIRequst<int>();
            var code = request.Codeme.Split(',').Select(x=>Convert.ToInt32(x)).ToList();

            var side = _workorderRepository.GetAll().Where(x => code.Contains(x.WorkId)).ToList();
            foreach (var item in side)
            {
                item.isDel = true;
            }
            var ment = await _workorderRepository.UpdateRange(side);
            if (ment > 0)
            {
                result.Code = RequstCode.成功;
                result.Msg = RequstCode.成功.ToString();

            }
            return await Task.FromResult(result);
        }
    }
}
